import axios from 'axios'
import { ElMessage } from 'element-plus'

// 配置请求拦截器
axios.interceptors.request.use(
	(config) => {
		console.log("--",config.params)
		config.headers['user_token_header'] = localStorage.getItem('token')
		return config
	},
	(err) => {
		return Promise.reject(err)
	}
)

const createAxios = (methods = 'get', url, date) => {
	let type = methods.toUpperCase()

	return new Promise((res, rej) => {
		axios({
			method: type,
			url: url,
			params: date,
			// data: type==="POST"? date:"",
		}).then(({ data }) => {
			if (data.code === 200) {
				ElMessage({
					message: '请求成功',
					type: 'success',
					duration: 1000,
				})
				res(data)
			}
			if (data.code === -1) {
				ElMessage({
					message: data.errMsg,
					type: 'warning',
					duration: 1000,
				})
				rej(data.errMsg)
			}
		})
	})
}

export default createAxios
